package imanager.showcases.agenda.client;

import imanager.showcases.agenda.entity.Compra;

import java.util.ArrayList;
import java.util.Collection;

import com.googlecode.imanager.client.Container;
import com.googlecode.imanager.client.Document;
import com.googlecode.imanager.client.Panel;
import com.googlecode.imanager.client.Table;
import com.googlecode.imanager.client.event.EvaluationPhase;
import com.googlecode.imanager.client.event.PhaseEvent;
import com.googlecode.imanager.client.layout.Overflow;
import com.googlecode.imanager.client.util.Anchors;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public class ListaComprasDocument extends Document {

    private static final long serialVersionUID = 1L;

    protected ListaComprasItem header;
    protected ListaComprasFooter footer;
    protected Table detail;
    
    public ListaComprasDocument() {
        setOverflow(Overflow.HIDDEN, Overflow.HIDDEN);
        setSize(570, 200);
        initializeControls();
        setTitle("Tabela de Compras");
        addListaCompras(getListaCompras());
    }
    
    public ListaComprasDocument(Container parent) {
        this();
        setParent(parent);
    }
    
    protected void initializeControls() {
        header = new ListaComprasItem(this);
        footer = new ListaComprasFooter(this);
        detail = new Table(this);
        
        header.setColumnValues("Produto", "Quantidade", "Valor Unit.", "Sub-Total");
        header.setAnchors(Anchors.HORIZONTAL_TOP);
        header.setHeight(header.getHeight() + 5);
        header.setRight(0);
        
        footer.setAnchors(Anchors.HORIZONTAL_BOTTOM);
        footer.setPosition(0, null, 0, 0);
        
        detail.setPosition(0, header.getHeight(), 0, footer.getHeight());
        detail.setAnchors(Anchors.STRETCH);
        detail.setHeader(header);
        detail.setFooter(footer);
    }
    
    public void clearListaCompras(){
        detail.getChildren().clear();
    }
    
    public void addListaCompras(Collection<Compra> listaCompras){
        for (Compra compra : listaCompras){
            new ListaComprasItem(detail).setCompra(compra);
        }
    }
    
    private Collection<Compra> getListaCompras() {
        Collection<Compra> l = new ArrayList<Compra>();
        for (int i=1; i<=50; i++){
            l.add(new Compra((long)i, (long)i, (long)i, i, i + 0.01*i));
        }
        return l;
    }
    
    @Override
    protected void onPhaseEvent(PhaseEvent e) {
        super.onPhaseEvent(e);
        
        if (e.getEventType() == EvaluationPhase.DOCUMENT_START){
            header.setStyle(getDefaultTheme().getStyle(Panel.class).clone());
            
            if (getImplementationType().isReport()){
                footer.setVisible(false);
            } else {
                footer.setVisible(true);
            }
        }
    }
    
    @Override
    public ListaComprasItem getHeader() {
        return header;
    }

    @Override
    public ListaComprasFooter getFooter() {
        return footer;
    }

    public Table getDetail() {
        return detail;
    }

}